<template>
    <el-dialog
        :visible.sync="dialogVisible"
        title="分类管理"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        width="600px"
    >
        <el-form ref="formRef" :model="formData" :rules="rules" label-width="80px">
            <el-form-item label="分类名称" prop="articleTypeName">
                <el-input v-model="formData.articleTypeName"/>
            </el-form-item>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="列表页" prop="listPath">
                        <el-select v-model="formData.listPath" placeholder="请选择">
                            <el-option key="/list" label="/list" value="/list"/>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="详情页" prop="detailPath">
                        <el-select v-model="formData.detailPath" placeholder="请选择">
                            <el-option key="/detail" label="/detail" value="/detail"/>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>

            <el-form-item label="摘要" prop="summary">
                <el-input type="textarea" v-model="formData.summary"/>
            </el-form-item>

            <el-row>
                <el-col :span="8">
                    <el-form-item label="图一" prop="image1">
                        <el-upload
                            class="avatar-uploader"
                            :action="apiHost + '/file/upload'"
                            :show-file-list="false"
                            :on-success="handleSuccess1"
                            :headers="headers"
                        >
                            <div v-if="formData.image1">
                                <img :src="formData.image1" class="avatar">
                            </div>
                            <i v-else class="el-icon-plus avatar-uploader-icon"/>
                        </el-upload>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="图二" prop="image2">
                        <el-upload
                            class="avatar-uploader"
                            :action="apiHost + '/file/upload'"
                            :show-file-list="false"
                            :on-success="handleSuccess2"
                            :headers="headers"
                        >
                            <div v-if="formData.image2">
                                <img :src="formData.image2" class="avatar">
                            </div>
                            <i v-else class="el-icon-plus avatar-uploader-icon"/>
                        </el-upload>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="图三" prop="image3">
                        <el-upload
                            class="avatar-uploader"
                            :action="apiHost + '/file/upload'"
                            :show-file-list="false"
                            :on-success="handleSuccess3"
                            :headers="headers"
                        >
                            <div v-if="formData.image3">
                                <img :src="formData.image3" class="avatar">
                            </div>
                            <i v-else class="el-icon-plus avatar-uploader-icon"/>
                        </el-upload>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>

        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false" style="margin-right: 20px;">取 消</el-button>
            <el-popconfirm title="确定保存分类吗？" @onConfirm="submitForm()">
                <el-button slot="reference" type="primary">保存</el-button>
            </el-popconfirm>
        </div>
    </el-dialog>
</template>

<script>
import {ArticleTypeApi} from '@/api/cms/ArticleType'
import {Common} from '@/utils/common'
import {getToken} from '@/utils/auth'

export default {
    name: 'CmsArticleUpdate',
    components: {},
    data() {
        return {
            formData: {
                id: 0,
                parentId: 0,
                articleTypeName: '',
                summary: '',
                listPath: '',
                detailPath: '',
                image1: '',
                image2: '',
                image3: ''
            },
            rules: {
                articleTypeName: [
                    {required: true, message: '请输入分类名称', trigger: 'blur'},
                    {min: 1, max: 60, message: '长度在 1 到 60 个字符', trigger: 'blur'}
                ]
            },
            dialogVisible: false,
            apiHost: process.env.VUE_APP_BASE_API,
            headers: {'X-Token': getToken()}
        }
    },
    methods: {
        // 显示组件
        showUpdate(data) {
            this.dialogVisible = true
            this.$nextTick(() => {
                if (this.$refs['formRef'] !== undefined) {
                    this.$refs['formRef'].resetFields()
                }

                if (data.id > 0) {
                    this.formData = Common.assign(this.formData, data)
                } else {
                    this.formData.id = 0
                    this.formData.parentId = data.parentId
                }
            })
        },

        // 提交表单
        submitForm() {
            const that = this
            that.$refs['formRef'].validate((valid) => {
                if (valid) {
                    if (that.formData.id > 0) {
                        ArticleTypeApi.edit(that.formData)
                            .then((response) => {
                                that.dialogVisible = false
                                that.$emit('refresh',that.formData.parentId)
                            })
                    } else {
                        ArticleTypeApi.add(that.formData)
                            .then((response) => {
                                that.dialogVisible = false
                                that.$emit('refresh',that.formData.parentId)
                            })
                    }
                }
            })
        },
        handleSuccess1(e) {
            this.formData.image1 = e.data
        },
        handleSuccess2(e) {
            this.formData.image2 = e.data
        },
        handleSuccess3(e) {
            this.formData.image3 = e.data
        },
    }
}
</script>
